﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Admin_jiaju.Application.Students;
using Admin_jiaju.Application.Students.Dtos;
using Admin_jiaju.Core.Models;
using Admin_jiaju.Core.Repositories;
using Admin_jiaju.mvc.Controllers;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using Admin_jiaju.mvc.Models.Students;
using Microsoft.AspNetCore.Mvc.Rendering;


namespace Admin_jiaju.mvc.Areas.Admin.Controllers
{
    [Area("Admin")]
    public class ProductController : BaseController
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IRepository<Product, int> _repository;
        private readonly IRepository<ProductType, int> _Typerepository;
        private readonly IAdmin_jiajuService _admin_JiajuService;
        private readonly IMapper _mapper;

        public ProductController(ILogger<HomeController> logger,
             IRepository<Product, int> repository,
              IRepository<ProductType, int> Typerepository,
             IAdmin_jiajuService admin_JiajuService,
             IMapper mapper

        )
        {
            _logger = logger;
            _repository = repository;
            _Typerepository = Typerepository;
            _admin_JiajuService = admin_JiajuService;
            _mapper = mapper;
        }
        [HttpGet]
        public async Task<IActionResult> list(StudnetInputDto studnetInputDto)
        {
            var quer = _repository.GetAll();
            if (!string.IsNullOrEmpty(studnetInputDto.Name))
            {
                quer = quer.Where(t => t.Name.Contains(studnetInputDto.Name));
            }
            if (!string.IsNullOrEmpty(studnetInputDto.XingHao))
            {
                quer = quer.Where(t => t.XingHao == (studnetInputDto.XingHao));
            }
            if (!string.IsNullOrEmpty(studnetInputDto.CaiZhi))
            {
                quer = quer.Where(t => t.CaiZhi == (studnetInputDto.CaiZhi));
            }
            if (!string.IsNullOrEmpty(studnetInputDto.HomeFlag))
            {
                quer = quer.Where(t => t.HomeFlag == (studnetInputDto.HomeFlag));
            }
            var count = quer.Count();


            quer = quer.OrderBy(t=>t.Id).Include(t => t.ProductType).Skip((studnetInputDto.Page - 1) * studnetInputDto.Limit).Take(studnetInputDto.Limit);
            var stuts = await quer.AsNoTracking().ToListAsync();
            //AsNoTracking不对实体类进行追踪
                                                 //var dtos = new PagedResultDto<Product>
                                                 //{
                                                 //    TotalCount = count,
                                                 //    Page = productPageDtos.Page,
                                                 //    Limit = productPageDtos.Limit,
                                                 //    Data = stuts,
                                                 //    FilterText = productPageDtos.FilterText,
                                                 //    Sorting = productPageDtos.Sorting
                                                 //};
            return Json(ListResult(_mapper.Map<List<ProductView>>(stuts), count));
           // return Json(ListResult(stuts, count));
        }

        public IActionResult Index()
        {
            return View();
        }
        [HttpGet]
        public async Task<IActionResult> Add()
        {
            ViewBag.TypeName = new MultiSelectList(await _Typerepository.GetAllListAsync(), "Id", "Name");
            //ViewBag.A =await _Typerepository.GetAllListAsync();
            return View();
        }
        [HttpPost]
        public async Task<IActionResult> Adddel(ProductView productView)
        {
            await _repository.InsertAsync(_mapper.Map<Product>(productView));
            bool a = await _repository.SavesAsync();
            if (a == true)
            {
                return Json(new { success = true, msg = "新增成功" });
            }
            else
            {
                return Json(new { success = false, msg = "新增失败" });
            }
        }
        [HttpGet]
        public async Task<IActionResult> Edit(int Id)
        {

            //通过Id获取数据详情
            var bannerDel = await _repository.FirstOrDefaultAsync(t => t.Id == Id);
            //把详细地址传到页面
            ViewBag.TypeName = new MultiSelectList(await _Typerepository.GetAllListAsync(), "Id", "Name");
            return View(_mapper.Map<ProductView>(bannerDel));
        }
        [HttpPost]
        public async Task<IActionResult> Edit(ProductView productView)
        {
            //编辑，请求数据库       
            await _repository.UpdateAsync(_mapper.Map<Product>(productView));
            bool a = await _repository.SavesAsync();
            if (a == true)
            {
                return Json(new { success = true, msg = "编辑成功" });
            }
            else
            {
                return Json(new { success = false, msg = "编辑失败" });
            }
        }
        [HttpPost]
        public async Task<IActionResult> DelPro(int Id)
        {
            var a = await _repository.FirstOrDefaultAsync(t => t.Id == Id);
            if (a == null)
            {
                return Json(new { success = false, msg = "该用户不存在无法删除" });
            }
            await _repository.DeleteAsync(t => t.Id == Id);
            bool n = await _repository.SavesAsync();
            if (n == true)
            {
                return Json(new { success = true, msg = "删除成功" });
            }
            else
            {
                return Json(new { success = false, msg = "删除失败" });
            }


        }


    }
}
